# -*- coding: utf-8 -*-
"""
Module Description: 装备系统
Time : 2021/5/10 
Author : sxj
File : equipment_utils.py
"""
from collections import defaultdict

from pyshare.utils.dict_utils import chunks
from pyshare.core.context import temporary_ctx
from pyshare.const.enums.equipment_enum import MaxPreparedNum
from pyshare.dal.repository_impl import EquipmentRoleRepository


class EquipmentDataType(object):
    """
    装备信息数据类型
    """
    # 字典
    DictType = "dict"
    # 列表
    ListType = "list"


def equipment_data_dict_to_list(equipment_dict):
    """
    装备信息字典格式转为列表
    :param equipment_dict:
    :return: [equipment_id, quality_id, slot, equipment_uid]
    """
    equipment_list = [equipment_dict['EquipmentId'], equipment_dict['QualityId'], equipment_dict['Slot'],
                      equipment_dict.get('EquipmentUid', '')]
    return equipment_list


def equipment_data_list_to_dict(equipment_list):
    """
    装备信息列表格式转字典
    :param equipment_list: [equipment_id, quality_id, slot, equipment_uid]
    :return:
    """
    equipment_id = equipment_list[0]
    quality_id = equipment_list[1]
    slot = equipment_list[2]
    equipment_uid = equipment_list[3]
    equipment_dict = {
        "EquipmentId": equipment_id,    # 装备id
        "QualityId": quality_id,        # 装备品质
        "Slot": slot,                   # 装备放置槽位
        "EquipmentUid": equipment_uid,  # 装备唯一id
    }
    return equipment_dict


def create_equipment_data(e_record, slot, need_uid, data_type):
    """
    生成装备信息
    :param e_record: 装备实例
    :param slot: 装备所佩戴槽位
    :param need_uid: 是否需要装备uid, 若不需要装备uid, 则equipment_uid='',节省空间
    :param data_type: 数据类型 dict:字典, list:列表
    :return:
    """
    # 获取装备唯一id
    if need_uid:
        equipment_uid = e_record.equipment_uid
    else:
        equipment_uid = ''

    if data_type == EquipmentDataType.DictType:
        equipment_dict = {
            "EquipmentId": e_record.equipment_id,  # 装备id
            "QualityId": e_record.quality_id,      # 装备品质
            "Slot": slot,                          # 装备放置槽位
            "EquipmentUid": equipment_uid,         # 装备唯一id
        }
        return equipment_dict
    else:
        equipment_list = [e_record.equipment_id, e_record.quality_id, slot, equipment_uid]
        return equipment_list


def get_minister_list_equipment(rid, server_id, minister_record_list, need_uid=True, data_type=EquipmentDataType.DictType):
    """
    批量获取大臣佩戴装备信息
    :param rid:
    :param server_id: 区服id
    :param minister_record_list: 大臣实例列表
    :param need_uid: 是否需要装备uid, 若不需要装备uid, 则equipment_uid='',节省空间
    :param data_type: 装备信息数据类型 dict:字典, list:列表
    :return:
    """
    all_equipment_uid = list()     # 大臣所有装备uid
    minister_record_dict = dict()  # {minister_id: minister_record}
    for minister_record in minister_record_list:
        minister_record_dict[minister_record.cfg_id] = minister_record
        all_equipment_uid.extend(minister_record.equipment_dict.values())

    # 大臣装备信息字典
    # data_type="dict":
    # {"minister_id": [{"EquipmentId": 装备id, "QualityId": 装备品质, "Slot": 槽位, "EquipmentUid": 装备uid}, {}, {}]}
    # data_type="list":
    # {"minister_id": [[equipment_id, quality_id, slot, equipment_uid], [], []]
    minister_equipment_dict = defaultdict(list)

    # 获取大臣装备信息
    with temporary_ctx(server_id=server_id):
        equipment_record_list = list()
        for short_uid_list in chunks(all_equipment_uid, MaxPreparedNum):
            short_record_list = EquipmentRoleRepository.filter_by_rid_uid_list(rid, short_uid_list)
            equipment_record_list.extend(short_record_list)

    # 拼接大臣佩戴装备信息
    for e in equipment_record_list:
        # 获取装备槽位
        minister_record = minister_record_dict[e.minister_id]
        slot = 0
        for t_slot, equipment_uid in minister_record.equipment_dict.items():
            if equipment_uid == e.equipment_uid:
                slot = t_slot
                break

        equipment_data = create_equipment_data(e, slot, need_uid, data_type)
        minister_equipment_dict[str(e.minister_id)].append(equipment_data)
    return minister_equipment_dict
